package own.stu.jobgib.playown.alg.leetcode.editor.cn;

//给定两个整数数组a和b，计算具有最小差绝对值的一对数值（每个数组中取一个值），并返回该对数值的差 
//
// 
//
// 示例： 
//
// 
//输入：{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
//输出：3，即数值对(11, 8)
// 
//
// 
//
// 提示： 
//
// 
// 1 <= a.length, b.length <= 100000 
// -2147483648 <= a[i], b[i] <= 2147483647 
// 正确结果在区间 [0, 2147483647] 内 
// 
// Related Topics 数组 双指针 
// 👍 31 👎 0


import java.util.Arrays;

public class SmallestDifferenceLcci{
    public static void main(String[] args) {
        Solution solution = new SmallestDifferenceLcci().new Solution();
        System.out.println(solution.smallestDifference(new int[]{-2147483648, 1, 3, 15, 11, 2}, new int[]{2147483647, 23, 127, 235, 19, 8}));
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int smallestDifference(int[] a, int[] b) {
            if(a == null || a.length == 0 || b == null || b.length == 0){
                return -1;
            }

            Arrays.sort(a);
            Arrays.sort(b);

            Long ans = Long.valueOf(Integer.MAX_VALUE);
            int i = 0;
            int j = 0;
            while(i < a.length && j < b.length){
                ans = Math.min(ans, Math.abs((long)a[i] - (long)b[j]));
                if(a[i] < b[j]){
                    i ++;
                }else{
                    j ++;
                }
            }

            return ans.intValue();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
